package ecom.beans.dao;

import ecom.beans.StockBean;
import ecom.beans.exception.DAONotUniqueException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class StockDAO extends DAO {

    public static void saveStock(StockBean stock) throws DAONotUniqueException {
        if (getByIdProductstoreAndIdProduct(stock.getProductBean().getIdProduct(), stock.getProductstoreBean().getIdProductStore()) != null) {
            throw new DAONotUniqueException("This stock already exist");
        }
        saveOrUpdate(stock);
    }

    public static void deleteStock(StockBean stock) {
        delete(stock);
    }

    public static void updateStock(StockBean stock) {
        saveOrUpdate(stock);
    }

    public static Set getAllStocks(String order) {
        return fetchAll("StockBean.findAll", null, order);
    }

    public static StockBean getByIdProductstoreAndIdProduct(Long idProduct, Long idProductstore) {
        List parameters = new ArrayList();
        parameters.add("idProduct");
        parameters.add(idProduct);
        parameters.add("idProductstore");
        parameters.add(idProductstore);
        return (StockBean) fetchOne("StockBean.findByPK", parameters);
    }

    public static Set<StockBean> getByIdProduct(Long idProduct, String order) {
        List parameters = new ArrayList();
        parameters.add("idProduct");
        parameters.add(idProduct);
        return fetchAll("StockBean.findByIdProduct", parameters, order);
    }

    public static Set<StockBean> getByIdProductstore(Long idProductstore, String order) {
        List parameters = new ArrayList();
        parameters.add("idProductstore");
        parameters.add(idProductstore);
        return fetchAll("StockBean.findByIdProductstore", parameters, order);
    }

    public static Set<StockBean> getBestTop(String order, Integer limit) {
        return fetchAllLimit("StockBean.bestTop", null, order, limit);
    }

    public static Set<StockBean> getByIdProductcategory(Long idProductcategory, String order) {
        List parameters = new ArrayList();
        parameters.add("idProductcategory");
        parameters.add(idProductcategory);
        return fetchAll("StockBean.findByIdProductcategory", parameters, order);
    }

    public static Set<StockBean> getByIdProductcategoryAndIdProductstore(Long idProductcategory, Long idProductstore, String order) {
        List parameters = new ArrayList();
        parameters.add("idProductcategory");
        parameters.add(idProductcategory);
        parameters.add("idProductstore");
        parameters.add(idProductstore);
        return fetchAll("StockBean.findByIdProductcategoryAndIdProductstore", parameters, order);
    }

    public static Set<StockBean> getByProductNameOrDescription(String name, String description, String order) {
        List parameters = new ArrayList();
        parameters.add("name");
        parameters.add("%" + name + "%");
        parameters.add("description");
        parameters.add("%" + description + "%");
        return fetchAll("StockBean.findByNameOrDescription", parameters, order);
    }
}
